package com.jd.financeweb.dao;

import com.jd.financeweb.pojo.dto.GetOverDueDTO;
import com.jd.financeweb.pojo.dto.GetRepayStatusDTO;
import com.jd.financeweb.utils.BaseDao;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class RepayDao extends BaseDao {

    /**
     * 根据 身份证号 查询 逾期还款的产品
     *
     * @param idNo
     * @return
     */
    public List<GetOverDueDTO> getUserOverDue(String idNo) {
        List<GetOverDueDTO> getOverDueDTOS = new ArrayList<>();
        String sql = "SELECT b_loan.product_name as cpName,(b_repay.exp_money-b_repay.repay_money) as shengyMoney,TIMESTAMPDIFF(DAY,b_repay.exp_time,CURRENT_TIME) AS overdue FROM b_loan LEFT JOIN b_repay ON b_repay.dk_id=b_loan.dk_id WHERE b_loan.dku_no= ? AND b_repay.exp_time < CURRENT_DATE AND b_repay.exp_money>b_repay.repay_money";
        try {
            super.executeQuery(sql, idNo);
            while (resultSet.next()) {
                GetOverDueDTO getOverDueDTO = new GetOverDueDTO();
                getOverDueDTO.setCpName(resultSet.getString("cpName"));
                getOverDueDTO.setShengvMoney(resultSet.getBigDecimal("shengyMoney"));
                getOverDueDTO.setOverdue(resultSet.getInt("overdue"));

                getOverDueDTOS.add(getOverDueDTO);
            }
        } catch (Exception e) {
            log.error("根据身份证号查询逾期产品信息发生了意料之外的错误！");
            e.printStackTrace();
        } finally {
            //关闭连接
            super.closeAll(connection, pstmt, resultSet);
        }
        return getOverDueDTOS;
    }

    /**
     * 根据  公司 编号 查询 逾期还款的产品
     *
     * @param cpNo
     * @return
     */
    public List<GetOverDueDTO> getCompOverDue(String cpNo) {
        List<GetOverDueDTO> getOverDueDTOS = new ArrayList<>();
        String sql = "SELECT b_loan.product_name as cpName,(b_repay.exp_money-b_repay.repay_money) as shengyMoney,TIMESTAMPDIFF(DAY,b_repay.exp_time,CURRENT_TIME) AS overdue FROM b_loan LEFT JOIN b_repay ON b_repay.dk_id=b_loan.dk_id WHERE b_loan.dkc_no= ? AND b_repay.exp_time < CURRENT_DATE AND b_repay.exp_money>b_repay.repay_money";
        try {
            super.executeQuery(sql, cpNo);
            while (resultSet.next()) {
                GetOverDueDTO getOverDueDTO = new GetOverDueDTO();
                getOverDueDTO.setCpName(resultSet.getString("cpName"));
                getOverDueDTO.setShengvMoney(resultSet.getBigDecimal("shengyMoney"));
                getOverDueDTO.setOverdue(resultSet.getInt("overdue"));

                getOverDueDTOS.add(getOverDueDTO);
            }
        } catch (Exception e) {
            log.error("根据公司编号查询逾期产品信息发生了意料之外的错误！");
            e.printStackTrace();
        } finally {
            //关闭连接
            super.closeAll(connection, pstmt, resultSet);
        }
        return getOverDueDTOS;
    }

    /**
     * 还款
     *
     * @param date
     * @param bigmoney
     * @param card
     * @param uName
     * @param hkId
     * @return
     */
    public int insertRecord(Date date, BigDecimal bigmoney, String card, String uName, Integer hkId) {
        String sql = "update  b_repay SET hk_date = ?, repay_money = ? , card_no =?, hk_uname = ? WHERE hk_id = ?";
        int i = 0;
        try {
            i = super.executeUpdate(sql, date, bigmoney, card, uName, hkId);
        } catch (Exception e) {
            log.error("根据公司编号查询逾期产品信息发生了意料之外的错误！");
            e.printStackTrace();
        } finally {
            //关闭连接
            super.closeAll(connection, pstmt, resultSet);
        }
        return i;
    }

    /**
     * 获取所有贷款当月还款状态（分页）
     *
     * @param page
     * @param limit
     * @return 当月贷款
     */
    public List<GetRepayStatusDTO> getRepayStatus(int page, int limit) {
        List<GetRepayStatusDTO> getRepayStatusDTOList = new ArrayList<>();
        String sql = "SELECT b_repay.hk_id as cpid ,b_loan.dku_name as cpname ,b_repay.exp_money as jine ,b_repay.hk_status as zhuangtai  FROM b_repay LEFT JOIN b_loan ON b_loan.dk_id = b_repay.dk_id  WHERE MONTH ( b_repay.exp_time ) = MONTH (NOW()) limit ?,?";
        try {
            super.executeQuery(sql, (page - 1) * limit, limit);
            while (resultSet.next()) {
                GetRepayStatusDTO getRepayStatusDTO = new GetRepayStatusDTO();
                getRepayStatusDTO.setCpid(resultSet.getInt("cpid"));
                getRepayStatusDTO.setCpname(resultSet.getString("cpname"));
                getRepayStatusDTO.setJine(resultSet.getBigDecimal("jine"));
                getRepayStatusDTO.setZhuangtai(resultSet.getInt("zhuangtai"));
                getRepayStatusDTOList.add(getRepayStatusDTO);
            }
        } catch (Exception e) {
            log.error("获取所有贷款当月还款状态（分页）！");
            e.printStackTrace();
        } finally {
            //关闭连接
            super.closeAll(connection, pstmt, resultSet);
        }
        return getRepayStatusDTOList;
    }
}
